<?php

require_once realpath (dirname (__FILE__) . '//..//pages.php');
require_once CLASS_DATABASE;
require_once CLASS_RECORD;
require_once INC_DEFINES;
require_once INC_FUNCTIONS;

define ('ACTION_DELETE', 'delete');
define ('ACTION_INSERT', 'insert');
define ('ACTION_SELECT', 'select');
define ('ACTION_UPDATE', 'update');

/**
 * Description of User
 *
 * @author Andrea
 */
class User extends Record {

    /**
     *
     * @var boolean its value is set true if already registered 
     */
    private $registered = false;

    /**
     * Creates a new User
     * 
     */
    public function __construct ($values) {
        $data = array ();
        if (is_array ($values)) {
            foreach ($values as $key => $value) {
                $add = true;
                switch ($key) {
                    case 'IDuser':
                        $key = 'ID';
                        break;
                    case 'last_login':
                    case 'password':
                    case 'username':
                        break;
                    case 'ID':
                        if (!isset ($values['IDuser'])) {
                            break;
                        }
                    default:
                        $add = false;
                }
                if ($add) {
                    $data[$key] = $value;
                }
            }
        }
        $this->rebuildWhere ();
        parent::__construct ('user', $data, $this->where);
    }

    /**
     * This method unsets all the variables of this object 
     */
    public function __destruct () {
        foreach ($this as $key => $value) {
            unset ($this->$key);
        }
    }

    /**
     * This methods is used to know if the user can do some actions
     * @param string $action the action the user required to do
     * @param string $what the table on which asks for permission
     * @return boolean true if user is allowed to do that action
     */
    public function can ($action, $what) {
        $this->reload ();
        if (isset ($this->values['ID'])) {
            $db = openConnection ();
            $where = "user.ID=possiede.IDuser AND permesso.ID=possiede.IDpermesso AND permesso.action='$action' AND permesso.what='$what' AND user.ID='" . $this->values['ID'] . "'";
            $result = $db->select (
                    array ('permesso', 'possiede', 'user'), 'permesso.action', $where, null, null, 1, true
            );
            if ($result) {
                $row = $db->fetchAssoc ($result);
                if ($row) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return false;
    }

    public function delete ($where = null) {
        if (isset ($this->values['password'])) {
            $this->values['password'] = "PASSWORD('" . $this->values['password'] . "')";
        }
        return parent::delete ($where);
    }

    public function exist ($what = "Don't use this param!") {
        return $this->isRegistered ();
    }

    public function insert () {
        if (isset ($this->values['password'])) {
            $this->values['password'] = "PASSWORD('" . $this->values['password'] . "')";
        }
        return parent::insert ();
    }

    /**
     * This method is used to know if the is already registered
     * 
     * Searches for a record that contains this user's username and password
     * 
     * @return boolean true if and only if the user is registered
     */
    public function isRegistered () {
        $db = openConnection ();
        $row = $db->fetchAssoc ($db->select ('user', array ('ID'), "username='" . $this->values['username'] . "' AND password=PASSWORD('" . $this->values['password'] . "')", null, null, null, null));
        if ($row) {
            $this->values['ID'] = $row['ID'];
            $this->registered = true;
            return true;
        }
        $this->registered = false;
        return false;
    }

    public function rebuildWhere () {
        if (isset ($this->values['ID'])) {
            $this->where = 'ID=' . $this->values['ID'];
        } else {
            if (isset ($this->values['username']) && isset ($this->values['password'])) {
                $this->where = "username='" . $this->values['username'] . "' AND password=PASSWORD('" . $this->values['password'] . "')";
            }
        }
    }

    public function reload ($where = null) {
        parent::reload ($where);
        if ($this->generate_where) {
            $this->rebuildWhere ();
        }
    }

    /**
     * This method sets a new username
     * 
     * @param string $username the new username
     */
    public function setUsername ($username) {
        $this->values['username'] = $username;
    }

    /**
     * This method sets a new password
     * @param string $password the new password
     */
    public function setPassword ($password) {
        $this->values['password'] = $password;
    }

    public function update ($where = null) {
        if (isset ($this->values['password'])) {
            $this->values['password'] = "PASSWORD('" . $this->values['password'] . "')";
        }
        return parent::update ($where);
    }

}

?>
